package com.yumi.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yumi.common.Result;
import com.yumi.entity.Announcement;
import com.yumi.service.EmployeeAnnouncementsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "员工公告", description = "公告的增删改查接口")
@RestController
@Slf4j
@RequestMapping("/api/announcements")
@RequiredArgsConstructor
public class EmployeeAnnouncementsController {

    private final EmployeeAnnouncementsService employeeAnnouncementsService;

    /**
     * 新增员工公告
     * @param announcement 公告信息
     * @return 操作结果
     */
    @Operation(summary = "新增员工公告", description = "创建新的员工公告")
    @PostMapping
    public Result postEmployeeAnnouncements(@RequestBody Announcement announcement) {
        log.info("新增员工公告：{}", announcement);
        employeeAnnouncementsService.save(announcement);
        return Result.success();
    }

    /**
     * 分页查询公告
     * @param page 页码
     * @param pageSize 每页大小
     * @param title 标题关键字
     * @param type 公告类型
     * @param active 是否有效
     * @return 分页结果
     */
    @Operation(summary = "分页查询公告", description = "根据条件分页查询公告列表")
    @GetMapping
    public Result<Page<Announcement>> pageQuery(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String title,
            @RequestParam(required = false) Integer type,
            @RequestParam(required = false) Boolean active) {
        
        log.info("分页查询公告：page={}, pageSize={}, title={}, type={}, active={}", 
                page, pageSize, title, type, active);
        
        Page<Announcement> pageResult = employeeAnnouncementsService.pageQuery(
                page, pageSize, title, type, active);
        
        return Result.success(pageResult);
    }
    
    /**
     * 查询公告详情
     * @param id 公告ID
     * @return 公告详情
     */
    @Operation(summary = "查询公告详情", description = "根据ID查询公告详细信息")
    @GetMapping("/{id}")
    public Result<Announcement> getAnnouncementById(@PathVariable Long id) {
        log.info("查询公告详情：id={}", id);
        Announcement announcement = employeeAnnouncementsService.getAnnouncementById(id);
        if (announcement != null) {
            return Result.success(announcement);
        }
        return Result.error("公告不存在");
    }
    
    /**
     * 修改公告
     * @param id 公告ID
     * @param announcement 公告信息
     * @return 操作结果
     */
    @Operation(summary = "修改公告", description = "根据ID修改公告信息")
    @PutMapping("/{id}")
    public Result updateAnnouncement(
            @PathVariable Long id,
            @RequestBody Announcement announcement) {
        
        log.info("修改公告：id={}, announcement={}", id, announcement);
        announcement.setId(id);
        employeeAnnouncementsService.updateAnnouncement(announcement);
        return Result.success();
    }
    
    /**
     * 删除公告
     * @param id 公告ID
     * @return 操作结果
     */
    @Operation(summary = "删除公告", description = "根据ID删除公告信息")
    @DeleteMapping("/{id}")
    public Result deleteAnnouncement(@PathVariable Long id) {
        log.info("删除公告：id={}", id);
        employeeAnnouncementsService.deleteAnnouncement(id);
        return Result.success();
    }
    
    /**
     * 置顶公告
     * @param id 公告ID
     * @return 结果
     */
    @PutMapping("/{id}/top")
    public Result topAnnouncement(@PathVariable Long id) {
        log.info("置顶公告：id={}", id);
        employeeAnnouncementsService.topAnnouncement(id);
        return Result.success();
    }
    
    /**
     * 取消置顶
     * @param id 公告ID
     * @return 结果
     */
    @PutMapping("/{id}/untop")
    public Result untopAnnouncement(@PathVariable Long id) {
        log.info("取消置顶：id={}", id);
        employeeAnnouncementsService.untopAnnouncement(id);
        return Result.success();
    }
    
    /**
     * 获取有效公告
     * @return 有效公告列表
     */
    @GetMapping("/active")
    public Result<List<Announcement>> getActiveAnnouncements() {
        log.info("获取有效公告");
        List<Announcement> announcements = employeeAnnouncementsService.getActiveAnnouncements();
        return Result.success(announcements);
    }
}